﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using T.ServiceComponent.I18N.Service;

namespace T.FAS.Runtime.Base.Entity
{
    /// <summary>
    /// 数据实体对象
    /// </summary>
    public class TEntity
    {
        /// <summary>
        /// TEntity构造函数
        /// </summary>
        public TEntity()
        {
            Attributes = new List<TAttribute>();
        }
        /// <summary>
        /// 元数据ID
        /// </summary>
        public string MetadataID { get; set; }
        /// <summary>
        /// 元数据Code
        /// </summary>
        /// <remarks>请求返回的DataTable的TableName</remarks>
        public string MetadataCode { get; set; }
        /// <summary>
        /// 元数据Name
        /// </summary>
        public string MetadataName { get; set; }
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }
        /// <summary>
        /// 引用路径表达式
        /// </summary>
        public string RefSchemaExpr { get; set; }
        /// <summary>
        /// 数据结构来源类型 例如 DQ、CDO、DO、EO等
        /// </summary>
        public TEntityType MetadataType { get; set; }
        /// <summary>
        /// TEntity构造函数
        /// </summary>
        /// <param name="type"></param>
        public TEntity(TEntityType type)
        {
            this.MetadataType = type;
        }
        /// <summary>
        /// 关联Entity与父类Entity的关联关系描述信息
        /// </summary>
        public TEntityRelation Relation { get; set; }
        /// <summary>
        /// 添加Entity属性
        /// </summary>
        /// <param name="attribute"></param>
        public void AddAttribute(TAttribute attribute)
        {
            if (Attributes.Exists(a => a.Equals(attribute.MetadataCode) && a.Code.Equals(attribute.Code)))
                return;
            Attributes.Add(attribute);
        }
        /// <summary>
        /// 数据返回类型  例如List\Record
        /// </summary>
        public DataType ResultType { get; set; }
        /// <summary>
        /// Entity属性集合
        /// </summary>
        public List<TAttribute> Attributes { get; set; }
        private List<TListMethod> _methods = null;
        /// <summary>
        /// 数据返回类型为List时,附带的操作方法列表
        /// </summary>
        public List<TListMethod> ListTypeMethods
        {
            get
            {
                if (this.ResultType == DataType.List &&( _methods == null || _methods.Count <= 0))
                {
                    _methods = GetDefaultListMethods();
                }
                return _methods;
            }
        }

        /// <summary>
        /// 创建数据返回类型为List时,附带的操作方法列表
        /// </summary>
        private List<TListMethod> GetDefaultListMethods()
        {
            var list = new List<TListMethod>();
            var listMethod = new TListMethod()
            {
                Code = "List",
                Description = "List",
                ReturnDataType = DataType.List,
                JSFunc = "#GetList",
                SortOrder = 0,
                SubMethods = new List<TListMethod>()
                {
                    new TListMethod() {Code = "Current", Description = $"{TermService.Current.GetText("T_FAS_fasr_Base_1930931563", "获取CurrentRowNumber行的记录")}", ReturnDataType = DataType.Record, JSFunc = "@Entity" + this.MetadataCode, SortOrder = 1, }, 
                    new TListMethod() {Code = "CurrentRowNumber", Description = $"{TermService.Current.GetText("T_FAS_fasr_Base_1099623862", "获取当前行号")}", ReturnDataType = DataType.Int, JSFunc = "#GetCurrentRowNumber", SortOrder = 2, }, 
                    new TListMethod() {Code = "Rows", Description = $"{TermService.Current.GetText("T_FAS_fasr_Base_1225880092", "获取数据行数")}", ReturnDataType = DataType.Int, JSFunc = "#GetRows", SortOrder = 3, }, 
                    new TListMethod() {Code = "Empty", Description = $"{TermService.Current.GetText("T_FAS_fasr_Base_1729006105", "数据是否为空")}", ReturnDataType = DataType.Boolean, JSFunc = "#IsEmpty", SortOrder = 4, }, 
                    new TListMethod() {Code = "Count", Description = "Count", ReturnDataType = DataType.Int, JSFunc = "#GetCount", SortOrder = 5, }
                }
            };
            list.Add(listMethod);
            return list;
        }
    }
}
